Skip to content

Reimplement 5 Map::Units update functions from gynt's units branch, under /GL (100%) - #393

Merged
gynt merged 9 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-B-round1-100
Nov 11, 2026
Merged

gynt merged 9 commits into
sourcehold:mainfrom
kotijekriv:reimpl/units-B-round1-100

Conversation

@kotijekriv

@kotijekriv kotijekriv commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Five more Map::Units update functions from gynt's units branch reimpl/map-units-updatefunctions (his draft #204), all at 100%. They are the ones his status marks "LTCG regs across calls": each matches only with /GL on it and on the callees it keeps registers across.

Function Address gynt's status His draft today
Units::UpdateBurningMan 0x0053BC20 66.1% 76.5%
Units::UpdateMiner 0x005401D0 51.8% 58.9%
Units::UpdateQuarryMason 0x00550A50 45.3% 62.2%
Units::UpdateDrunkard 0x00567320 48.3% 53.5%
Units::UpdateInnkeeper 0x00540EC0 50.7% 63.4%

"His draft today" is his file on our fork's copy of his branch (his 48b79a2 with main d5d965d merged in, plus a names-only build fix), in a full reccmp build with the Globals and the function resolvers on (2026-11-10). Each commit says which of his commits it builds on and carries him as co-author.

What made them match:

  • /GL (first commit). The original keeps a constant, or the unit's state, in edx across checkIfCitizenUnitIsAliveBasedOnState, which leaves edx alone (the first four). UpdateInnkeeper keeps the unit index in esi/edi across hasUnitReachedDestination, checkIfCitizenUnitIsAliveBasedOnState and getBuildingResourceAmountByUid. That only happens with link-time code generation, so the five go on cmake/compiler-flags-gl.txt with six UnitsState callees: checkIfCitizenUnitIsAliveBasedOnState, makeUnitStopWalkingByClearingPathProgressState, hasTunnelerNotFinishedDigging, hasUnitReachedDestination, commitUnitLocation and resumeMovementIfNoAttackTarget. All six are 100% on main and stay at 100% under /GL. UpdateInnkeeper's four BuildingsState callees are on the list already.
  • Three callees stay off the list, though they are 100% on main. setLastEncounteredTroopUnit makes no calls, so under /GL UpdateQuarryMason would skip the re-read of DAT_CurrentUnitSlotID that the original does after it (quick_diff 1.0000 -> 0.9901). setWorkplaceBuildingEntryAsTarget and resetUnitMovementState gain nothing, since they call into functions that are not under /GL.
  • Unit accesses through DAT_CurrentUnitSlotID. After each call it cannot see into, the original reads the current slot again instead of keeping unitID. So 10 to 15 statements per function index units[DAT_CurrentUnitSlotID::instance], as the original does; UpdateInnkeeper has no unitID local at all. Everything through unitID measures 0.62 to 0.91 in quick_diff (0.7154 BurningMan, 0.9013 Miner, 0.9059 QuarryMason, 0.6877 Drunkard, 0.6159 Innkeeper).
  • UpdateInnkeeper's ale tests were one step off in the draft: the original has jg/jge, so they are <= 150 and >= 400.

Measured on main 639c193. Main is merged in up to 02b5f47, whose newer commits add enum names only, and the changes here since the build are comment lines only.

  • Setup: the bundled MSVC1400-SP1 against the Steam Stronghold Crusader.exe (3bb0a8c1…5387ac5a), resolvers enabled locally (not committed), and sourcehold's reccmp with --resolve-wrapped-calls --ignore-security-cookie.
  • reccmp: a full build of the branch alone reads all five at 100.0% raw and normalized, and the ten /GL callees at 100%.
  • quick_diff for the measured comments is the variant that compares absolute global addresses, with the callees' .cpp files #included at the top of the caller. That emulates /GL for these functions to the instruction.
  • Checks: openshc-pr-check gives no errors, and status_regression.py finds no score below a known one.
  • Not run in game.

Header change (a commit of its own)

gynt's Ghidra sync 02b5f47 brought both values to main. Main is merged in (17a84e8), so this commit now matches main.

UnitState gets US_UNKNOWN_108 and US_UNKNOWN_120 (380b74f4b), in the US_UNKNOWN_<n> form, in place of (UnitState)108 and (UnitState)120; code generation is the same.

  • 120: UpdateBurningMan stores it (mov word ptr [ecx + 0x138880C], 0x78 at 0x0053BC9D: the units array at 0x0138854C plus state at 0x2C0) and tests it (cmp bp, 0x78 at 0x0053BCEA).
  • 108: UpdateMiner, UpdateQuarryMason and UpdateInnkeeper test it (cmp ax, 0x6c at 0x005402E3, cmp dx, 0x6c at 0x00550B66, cmp ax, 0x6c at 0x00541523).
  • The enum jumps from US_MELEE_ATTACK_WALL (107) to US_JESTER_ROAM_TO (109), and from US_STONE_DEATH_03 (116) to US_UNKNOWN_121.

Kept as they are, measured with quick_diff (each has a one-line comment)

Function Kept The other form
UpdateInnkeeper state 2: the success path in the then-arm, the unit removed after it an early return: 1.0000 -> 0.9723
UpdateInnkeeper state 3: the < 400 arm first, the full inn's commitUnitLocation after it 1.0000 -> 0.9822
UpdateInnkeeper in that arm: the accessible-storage path first 1.0000 -> 0.9881
UpdateQuarryMason the workplace read again after setLastEncounteredTroopUnit the workplace local: 1.0000 -> 0.8536
UpdateQuarryMason the blocks from the death state on as one else-if chain early returns: 1.0000 -> 0.9507
UpdateMiner the death, unit-is-alive and default blocks as one else-if chain early returns: 1.0000 -> 0.9385
UpdateDrunkard resourceToDeposit >= 6 for the next state (the original's setl) < 6 with the arms swapped: 1.0000 -> 0.9848

Proposed, not changed here

  • 25 (char) casts remain, on Unit::disappearFadeAlphaCountdown (+0x32E) and on fields of the AnimationFrameData tables. Both are byte in the headers, but the original compares them as signed (< 0, > 32), so without the cast < 0 is always false. Retyping them to char in Ghidra would let the casts go.
  • Frames_Shared_UnitClimbingUp (UnitPropertiesDefinedData +0x8B78, 33,748 bytes) is the whole AnimationFrameData table. All five read other animations from it (ANIM_Frames_UnitDeath, DAT_InnKeeperAnimationIndices, drunkard), so a name such as AnimationFrames would fit better.
  • DAT_UnitHasBecomeIdle is undefined4, but it is only ever stored 1 or tested against 0, so it could be BOOLEnum (as Reimplement 11 Map::Units functions from gynt's units branch (100%) #390 said too).
  • UpdateInnkeeper sets animationCycleNumber = 0 a second time when setDestinationForUnit fails (line 106), as the original does.
  • buildingIsAccessible returns int, so the tests on it stay == 0 / != 0.

Other PRs

🤖 Generated with Claude Code

kotijekriv and others added 7 commits November 11, 2026 11:59
…callees

UpdateBurningMan, UpdateMiner, UpdateQuarryMason, UpdateDrunkard and UpdateInnkeeper only match with link-time code
generation. The original keeps values in registers across the calls it could see into: a constant or the unit's
state in edx across checkIfCitizenUnitIsAliveBasedOnState (which leaves edx alone) in the first four, and the unit
index in esi/edi across hasUnitReachedDestination, checkIfCitizenUnitIsAliveBasedOnState and
getBuildingResourceAmountByUid in UpdateInnkeeper. After the calls it could not see into, it reads
DAT_CurrentUnitSlotID again.

The six UnitsState functions added here are 100% on main with their function resolvers on, and each stays at 100%
under /GL (full build of this branch alone). Who needs them:

- checkIfCitizenUnitIsAliveBasedOnState and makeUnitStopWalkingByClearingPathProgressState: all five;
- hasTunnelerNotFinishedDigging: UpdateBurningMan (its last 0.8 points) and UpdateDrunkard;
- hasUnitReachedDestination and commitUnitLocation: UpdateMiner, UpdateQuarryMason and UpdateInnkeeper;
- resumeMovementIfNoAttackTarget: UpdateQuarryMason and UpdateDrunkard.

UpdateInnkeeper's four BuildingsState callees are on main's list already and stay at 100% too: buildingIsAccessible,
getStorageBuildingForResourceTypeAndAmount, getBuildingResourceAmountByUid and addResourceToStockpile.

Three callees that are 100% on main stay off the list. setLastEncounteredTroopUnit (UpdateMiner, UpdateQuarryMason)
makes no calls, so under /GL the caller would know it leaves DAT_CurrentUnitSlotID alone and skip the re-read the
original does (with it in UpdateQuarryMason's translation unit: quick_diff 1.0000 -> 0.9901; UpdateMiner returns
right after the call and stays 1.0000). setWorkplaceBuildingEntryAsTarget and resetUnitMovementState
(UpdateInnkeeper) would gain nothing: they call on into functions that are not under /GL (buildingIsAccessible's
callees calculateCanPlayerUnitsNavigateToAreaFromArea and determineBuildingEntranceFromKeepArea, which are not
reimplemented yet, and commitPendingUnitPosition), so UpdateInnkeeper reads the slot again after them either way,
and with both in its translation unit its code is identical (quick_diff 1.0000). setDestinationForUnit,
computeLadderClimbPath and changeDestinationByAmount are not reimplemented yet.

quick_diff here is the variant that compares absolute global addresses, run with the callees' .cpp files #included
at the top of the caller; that emulates /GL for these functions to the instruction.

On reimpl/map-units-updatefunctions, gynt's status lines for these five read "LTCG regs across calls"
(UpdateInnkeeper: "LTCG_regs"), which is where this started.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Units::UpdateBurningMan (0x0053BC20) puts a unit into state 120 (mov word ptr [ecx + 0x138880C], 0x78 at 0x0053BC9D:
the units array at 0x0138854C plus state at 0x2C0) and tests for it (cmp bp, 0x78 at 0x0053BCEA). Units::UpdateMiner
(0x005401D0), Units::UpdateQuarryMason (0x00550A50) and Units::UpdateInnkeeper (0x00540EC0) test for state 108 (cmp
ax, 0x6c at 0x005402E3, cmp dx, 0x6c at 0x00550B66, cmp ax, 0x6c at 0x00541523). The enum goes from
US_MELEE_ATTACK_WALL (107) to US_JESTER_ROAM_TO (109) and from US_STONE_DEATH_03 (116) to US_UNKNOWN_121 (121); the
two are named in the US_UNKNOWN_<n> form. Used in place of (UnitState)108 and (UnitState)120; code generation is the
same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateBurningMan. Builds on gynt's bd4d1d5 ("66.14% LTCG regs across calls") as our fork's copy of his branch has
it, with main merged in and main's header renames followed (43a17df, names only), where his draft measures 76.5%
in reccmp (normalized 74.5%) in a full build with the Globals and the function resolvers on (2026-11-10). This
version is 100.0% (normalized 100.0%) in a full build of this branch alone, with /GL on it and on its callees
hasTunnelerNotFinishedDigging, checkIfCitizenUnitIsAliveBasedOnState and
makeUnitStopWalkingByClearingPathProgressState (first commit), which all stay at 100%.

The stores before the state read, the state-0 block, the unit-is-alive block and the three stores after
hasTunnelerNotFinishedDigging index the units through DAT_CurrentUnitSlotID. That keeps them in the original's order
and leaves edx for the constant 1 the original keeps across checkIfCitizenUnitIsAliveBasedOnState (everything
through unitID: quick_diff 1.0000 -> 0.7154, in one comment above unitID). State 120 is US_UNKNOWN_120 (header
commit), and computeLadderClimbPath's BOOLEnum is tested for truth.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateMiner. Builds on gynt's 5e96437 ("51.78% LTCG regs across calls") as our fork's copy of his branch has it,
with main merged in and main's header renames followed (43a17df, names only), where his draft measures 58.9% in
reccmp (normalized 58.9%) in a full build with the Globals and the function resolvers on (2026-11-10). This version
is 100.0% (normalized 100.0%) in a full build of this branch alone, with /GL on it and on its callees
checkIfCitizenUnitIsAliveBasedOnState, hasUnitReachedDestination, commitUnitLocation and
makeUnitStopWalkingByClearingPathProgressState (first commit), which all stay at 100%.

Ten unit accesses go through DAT_CurrentUnitSlotID, which keeps the original's load and store order and its
registers (everything through unitID: quick_diff 1.0000 -> 0.9013), and the death, unit-is-alive and default blocks
form one else-if chain, so the shared tail sits in the second arm as in the original (early returns: quick_diff
1.0000 -> 0.9385). The worker slot takes unitID without a (short) cast, and state 108 is US_UNKNOWN_108 (header
commit).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateQuarryMason. Builds on gynt's ec84561 ("45.27% LTCG regs across calls") as our fork's copy of his branch has
it, with main merged in and main's header renames followed (43a17df, names only), where his draft measures 62.2%
in reccmp (normalized 62.2%) in a full build with the Globals and the function resolvers on (2026-11-10). This
version is 100.0% (normalized 100.0%) in a full build of this branch alone, with /GL on it and on its callees
checkIfCitizenUnitIsAliveBasedOnState, hasUnitReachedDestination, commitUnitLocation,
makeUnitStopWalkingByClearingPathProgressState and resumeMovementIfNoAttackTarget (first commit), which all stay at
100%.

Twelve unit accesses go through DAT_CurrentUnitSlotID, which keeps the original's load and store order and its
registers (everything through unitID: quick_diff 1.0000 -> 0.9059). One of them is the workplace read at the end of
the disappear block, where the original reads the slot again after setLastEncounteredTroopUnit (the workplace local
there: quick_diff 1.0000 -> 0.8536). The blocks from the death state on form one else-if chain (early returns:
quick_diff 1.0000 -> 0.9507). The worker slot takes unitID without a (short) cast, and state 108 is US_UNKNOWN_108
(header commit).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateDrunkard. Builds on gynt's 171a691 ("48.31% LTCG regs across calls") as our fork's copy of his branch has
it, with main merged in and main's header renames followed (43a17df, names only), where his draft measures 53.5%
in reccmp (normalized 53.5%) in a full build with the Globals and the function resolvers on (2026-11-10). This
version is 100.0% (normalized 100.0%) in a full build of this branch alone, with /GL on it and on its callees
checkIfCitizenUnitIsAliveBasedOnState, hasTunnelerNotFinishedDigging, makeUnitStopWalkingByClearingPathProgressState
and resumeMovementIfNoAttackTarget (first commit), which all stay at 100%.

Fifteen unit accesses go through DAT_CurrentUnitSlotID, which keeps the original's load and store order and its
registers (everything through unitID: quick_diff 1.0000 -> 0.6877), and the next state comes from an if that tests
resourceToDeposit >= 6, which gives the original's setl (< 6 with the arms swapped: quick_diff 1.0000 -> 0.9848).
computeLadderClimbPath's BOOLEnum is tested for truth.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
UpdateInnkeeper. Builds on gynt's 6b8b0c2 ("50.68% LTCG_regs") as our fork's copy of his branch has it, with main
merged in and main's header renames followed (43a17df, names only), where his draft measures 63.4% in reccmp
(normalized 58.5%) in a full build with the Globals and the function resolvers on (2026-11-10). This version is
100.0% (normalized 100.0%) in a full build of this branch alone, with /GL on it and on its callees
checkIfCitizenUnitIsAliveBasedOnState, hasUnitReachedDestination, commitUnitLocation and
makeUnitStopWalkingByClearingPathProgressState (first commit) and buildingIsAccessible,
getStorageBuildingForResourceTypeAndAmount, getBuildingResourceAmountByUid and addResourceToStockpile (on main's
list), which all stay at 100%.

His draft had the ale-level tests one step off: < 150 and > 400 where the original has jg and jge, that is <= 150
and >= 400. Every unit access goes through DAT_CurrentUnitSlotID, which the original reads again after each call it
cannot see into (a unitID local: quick_diff 1.0000 -> 0.6159), and the substate wraps at >= 4.

Three tests that the draft wrote as early returns keep the success path in the then-arm and the failure after the
if, which is where the original puts the failure blocks (it jumps forward to them):

- state 2, ale fetched: when setWorkplaceBuildingEntryAsTarget fails, the unit is removed after the then-arm (an
  early return: quick_diff 1.0000 -> 0.9723);
- state 3, ale delivered: the < 400 then-arm comes first, and the full inn's commitUnitLocation after it (0.9822);
- inside that arm, the accessible-storage path comes first, and the other commitUnitLocation after it (0.9881).

The other failures return early, as in the draft: the unreached destination in states 2 and 3, the storage that is
not accessible after the serving animation and in state US_AIM_WEAPONUnk, and the full inn there.
setWorkplaceBuildingEntryAsTarget's BOOLEnum is tested for truth, the target slot takes storage without a (short)
cast, states 2 and 3 are US_STATE_2Unk and US_STATE_3Unk, and state 108 is US_UNKNOWN_108 (header commit).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the declaration/type change This requires changes to the generated/exported types or definitions. label Nov 11, 2026

@gynt gynt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes were just pushed upstream btw

kotijekriv and others added 2 commits November 11, 2026 12:28
Its Ghidra sync brings UnitState's US_UNKNOWN_108 and US_UNKNOWN_120, so the header commit now matches main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ate functions

gynt on sourcehold#393: "These comments can go!" The top comment of UpdateBurningMan, UpdateMiner, UpdateQuarryMason,
UpdateDrunkard and UpdateInnkeeper explained why their unit accesses index through DAT_CurrentUnitSlotID. Comments
only; the code is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the declaration/type change This requires changes to the generated/exported types or definitions. label Nov 11, 2026
@kotijekriv

Copy link
Copy Markdown
Contributor Author

Thanks! Main (02b5f47) is merged in (17a84e8), so US_UNKNOWN_108 and US_UNKNOWN_120 now come from your sync, and the header commit matches main. The DAT_CurrentUnitSlotID comments are gone from all five files (a57eb32).

The other one-line comments are still there: the three early-return ones in UpdateInnkeeper, the else-if chains in UpdateMiner and UpdateQuarryMason, the workplace re-read in UpdateQuarryMason and the >= 6 test in UpdateDrunkard. Should those go too?

@gynt

gynt commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Thanks! Main (02b5f47) is merged in (17a84e8), so US_UNKNOWN_108 and US_UNKNOWN_120 now come from your sync, and the header commit matches main. The DAT_CurrentUnitSlotID comments are gone from all five files (a57eb32).

The other one-line comments are still there: the three early-return ones in UpdateInnkeeper, the else-if chains in UpdateMiner and UpdateQuarryMason, the workplace re-read in UpdateQuarryMason and the >= 6 test in UpdateDrunkard. Should those go too?

Those are fine

@gynt
gynt added this pull request to the merge queue Nov 11, 2026
Merged via the queue into sourcehold:main with commit cbf4bc6 Nov 11, 2026
4 checks passed
kotijekriv added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 24, 2026
…er, BuySell, UpdateGhost and UpdateHopsFarmer (sourcehold#416)

* cmake: /GL for InGameMenu MainButtons render function

MenuItemRenderFunction_InGameMenu_MainButtons (0x004643F0, next commit) only matches with link-time code generation.
The original keeps the button's data pointer in ecx across ButtonGmData::getPictureNumberInGm (0x004630B0), which
leaves ecx alone, and reads the button's gm ID through it after the call; MSVC can only do that with both functions
under /GL. getPictureNumberInGm is already in this list on main.

MainButtons measures 100.0 with /GL (reccmp, all-on build on main f489b72: the Globals and every finished function
resolver on, so both functions' resolvers too). On main 3416498 it measured 80.8 without /GL, and 83.8 under /GL
with MainButtons' own resolver off: nothing then takes its address, and LTCG drops the load of its argument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* reimplement: SHC_3BB0A8C1_0x004643F0 100.0% Reimplemented

MenuItemRenderFunction_InGameMenu_MainButtons, rewritten from the binary with gynt's draft for the names
(e6a4dba, 7f2191d and b9a9e27 on his fork's reimpl/UI-MenuItems, 48.75% there; 793ac41 at 57.73% and
616eb44 at 47.63% on his reimpl/UI-MenuItems-development, 616eb44 being his newest reimplement commit on the file)
and our mechanical pass of it in sourcehold#328 (ee10c60, 49.0%; quick_diff 0.6164 on main). The function leaves sourcehold#328 with
this commit.

reccmp 100.0 (normalized 100.0) in an all-on build on main f489b72 under /GL (the cmake commit before this one),
which has its function resolver and getPictureNumberInGm's (0x004630B0) on; 80.8 without /GL (main 3416498). The original keeps the button's data pointer
in ecx across that call, which only LTCG can know is safe; without it the pointer has to survive the call some other
way, and the zero, the one and the picture offset go to ebx, ebp and edi in another order than the original's.

Fixes against the draft:
- In the barracks and the mercenary post the original greys the buttons above -10 but the scribe (71) and -4; the
  draft greyed those at -10 and below (616eb44 turned the bound round).
- The greys of -1, -5 and -6 set the flags and go on to the build-menu test and the draw, so the button is still drawn;
  the draft returned at once for -1 and for one -5 case.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* cmake: /GL for Roundtable's click handler and isAIPlayer

MenuItemActionHandler_Roundtable_Main (0x004AEA50, next commit) only matches with link-time code generation. It calls
GameSynchronyState::isAIPlayer (0x004010B0) for each seat, and isAIPlayer changes only eax; with both under /GL MSVC
knows that, and gives the handler the original's registers (eax for the chosen lord's seat, ecx for the button where
the seats are read). Without /GL the handler reads 87.2 in every source form tried.

isAIPlayer is 100% on main with its function resolver on and stays at 100.0 under /GL (reccmp, all-on build on main
f489b72, built with the handler). It is the only callee the handler needs there: on main 3416498, with the
handler's other five finished callees (resetMouseState2, nextRandomNumber1, playWAVSFX, scheduleSFXVariation,
shouldSoundXNotBePlaying) also under /GL, the result was the same 100.0. No other file in this list calls isAIPlayer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* reimplement: SHC_3BB0A8C1_0x004AEA50 100.0% Reimplemented

MenuItemActionHandler_Roundtable_Main, from gynt's draft (c53b2b9 and 18ddd79 on his fork's reimpl/UI-MenuItems,
61.09% there; 65013b9 at 66.46% and a04df2a at 67.08% on his reimpl/UI-MenuItems-development, a04df2a being
his newest reimplement commit on the file), our mechanical pass of it in sourcehold#328 (15884ad, 71.2%) and our own rewrite
from the binary at 87.2% (local, never pushed). The function leaves sourcehold#328 with this commit.

reccmp 100.0 (normalized 100.0) in an all-on build on main f489b72 under /GL (the cmake commit before this one),
which has its function resolver and isAIPlayer's on; 87.2 without /GL (main 3416498).

From the binary: the chosen lord's and the target's seats are read from DAT_RoundTableOrderArray (the decompiler had
them as mapSendRequestStatePerPlayer + button + 26), the group numbers are char reads into ints with no casts, the
highest group in use is a plain loop over players 1 to 8 (MSVC unrolls it into the original's eight copies, as it does
the group count of button 100), the new group is one local stored twice, and the buttons after the seat logic are an
if chain with returns. The last difference under /GL was which of eax and ecx held the chosen seat and the button: the
original's comes from reading the chosen lord's seat before the target's.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* reimplement: SHC_3BB0A8C1_0x00467040 100.0% Reimplemented

MenuItemActionHandler_BuildingAndStatusMenu_BuySellMenuButtonsAndHands, from gynt's draft (7cbb7b8 and fb51a06
on his fork's reimpl/UI-MenuItems, 83.17% there; 30ace91 at 83.81% on his reimpl/UI-MenuItems-development) and
our pass of it in sourcehold#328 (74bdd12, 83.8%; 60ea82a, 93.3%). The function leaves sourcehold#328 with this commit.

Register allocation only, per sourcehold#414, so the status line records 100.0. reccmp's own score is 93.9 in an all-on build
on main f489b72, and diff_triage's register-blind ratio is 1.000 (0.859 before renaming registers): every
remaining difference is a register name. Ten line pairs differ. In eight, the two `imul ..., 0x39f4` player-index
computations use ecx and edx the other way round and one load and one store use edx where the original uses eax.
The other two are jumps one byte off, because the original's `mov [global], eax` takes the shorter moffs encoding
that edx has not. Same instruction count, same mnemonics in the same order, same memory operands.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* reimplement: SHC_3BB0A8C1_0x00543B80 100.0% Reimplemented

Units::UpdateGhost, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (9fd4871 at 52.63%,
e248d10 at 60.35%), under /GL:
- owner is read once at the top through DAT_CurrentUnitSlotID::instance, into an int const local that the
  nonInteractiveCitizenCountUnk increment and findNextReachableDefensiveBuildingForPlayer's first argument use: the
  original loads it before the stateBasedSpeed store and keeps it in ecx up to that call (quick_diff with the /GL
  callees in the unit 0.9568 -> 0.9784; the local read through unitID 0.7071, a short local 0.5833);
- makeUnitStopWalkingByClearingPathProgressState at the end and hasUnitReachedDestination get the slot, and in
  US_IDLEUnk every statement from the destinationNeeded store after the timeGetTime test to the end of the state
  indexes units[] through it, as the original reads the slot again after playSFXAtLocation and timeGetTime. unitID
  is then not live across those calls, and the timeGetTime pointer gets esi as in the original (0.9424 -> 0.9568;
  starting one statement later 0.9429). The other statements use unitID or the slot as a greedy quick_diff search
  picked them one statement per step;
- states 108 and 2 are US_UNKNOWN_108 and US_STATE_2Unk;
- three stores in blocks that otherwise use unitID stay through the slot (no comment, per gynt on sourcehold#393): the
  US_IDLEUnk state store in US_DETERMINE_NEXT_STATEUnk (through unitID: quick_diff 0.9784 -> 0.9640), the
  field_0x30_animRelated store in US_MELEE_ATTACK (0.9712) and the clamp to 32 in state 2 (0.9712);
- compiler-flags-gl.txt lists UpdateGhost.cpp: 100% with the line, 49.3% without it, as hasUnitReachedDestination
  and makeUnitStopWalkingByClearingPathProgressState, both on main's list, have to be visible.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* reimplement: SHC_3BB0A8C1_0x00552F80 100.0% Reimplemented

Units::UpdateHopsFarmer, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (4c7f4b4 at 50.99%),
through an earlier version on our dev branch at 99.7%: every unit access goes through
DAT_CurrentUnitSlotID::instance with the worker-helper callees visible, the states form one else-if chain, and a
block-level quick_diff search put the storage lookup back on unitID; the solitary-or-not goods amount is an if/else
with the call in each arm; in the delivery state the can-store path is the then-arm of canBuildingStoreTheAmount
and the reachable-storage path the then-arm of buildingIsAccessible. Kept forms (quick_diff with the /GL
callees in the unit; the first and last with a one-line why, the slot indexing without, per gynt on sourcehold#393):
- the delivery state's else arm reads the slot into unitID again, as the original does (without it 0.9949);
- setDestinationForUnit in the walk-to-field state takes unitID, as the original keeps the slot across
  UpdateWorker_ConsiderTakingABreakUnk there (through the slot 0.9864);
- ownFarm reads workplaceBuildingID_1 again after the calls next to farm (reusing farm 0.9272).
Style on top, all the same code (0.9971 before and after): DNE_UNKNOWN_2 for (DestinationNeededEnum)2 and no using
line, no casts on the stores into short fields and on the coordinate operands, BOOLEnum results tested by
truthiness, ++/--, int const storage, no unused BOOLEnum include.

compiler-flags-gl.txt gets UpdateHopsFarmer.cpp and the three callees it needs visible, each 100% on main and
called by no other file on the list: ComputeGoodsProduced (quick_diff without it 0.9599),
IncrementAndOptionalUpdateAVValueRelated (0.8182) and SetStateToFreetimeWalking (0.9899); with no callee bodies at
all, i.e. off the list, 0.5950. UpdateWorker_ConsiderTakingABreakUnk makes no difference (0.9971 without it) and
stays off. reccmp in a build of main's whole /GL list with every resolver of those files on: UpdateHopsFarmer
99.7%, the three callees 100.0%, nothing else moved.

The one difference left is register allocation only (sourcehold#414): the owner pushed for
getBuildingThatCanStoreThisResource is loaded into eax where the original uses ecx, the same two instructions and
memory operand. Owner locals, an int or short cast, and reading it through unitID or the slot all tie or lose; a
symbol-count sweep (0..95 dummy typedefs, after the includes and at the top) reads 0.9971 at every count.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Gynt <gynt@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants